/*
 * Copyright (c) 2010, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ALKES_VERTEX_BUFFER_HELPER_H_INCLUDED_
#define ALKES_VERTEX_BUFFER_HELPER_H_INCLUDED_

#include "alkes/internal.h"
#include "alkes/gfx/IVertexBuffer.h"

namespace alkes {

struct VertexTypeTrait {
    uint32_t type;
    int32_t size;
};

class AL_EXPORT VertexBufferHelper
    : public IVertexBuffer
{
    AL_DECLARE_CLASS(VertexBufferHelper, IVertexBuffer);

public:

    /** Calculate vertex size from vertex traits.
    @param [in] traits
    @param [in] length
    @param [in] vertex_type
    @return
    */
    static int32_t getVertexSize(const VertexTypeTrait* traits, int32_t length, uint32_t vertex_type);

    /** Get offset of vertex type.
    @param [in] member
    @return
    @sa VertexType
    */
    virtual int32_t getOffsetOf(VertexType member);

    /** Get pointer of vertex array.
    @param [in] index
    @param [in] count
    @return
    */
    virtual void* getBuffer(int32_t index, int32_t count) = 0;

    /** Get vertex size.
    */
    virtual int32_t getVertexSize() = 0;

    /** Get vertex traits table.
    @param [out] length
    @return
    */
    virtual const VertexTypeTrait* getVertexTypeTraits(int32_t& length) = 0;

protected:
    VertexBufferHelper(uint32_t vertex_type);
    virtual ~VertexBufferHelper();

    virtual bool onSetVertex2D(int32_t index, int32_t count, const Vector2D* vertex);
    virtual bool onSetVertex3D(int32_t index, int32_t count, const Vector3D* vertex);
    virtual bool onSetNormal(int32_t index, int32_t count, const Vector3D* vertex);
    virtual bool onSetDiffuse(int32_t index, int32_t count, const Color* color);
    virtual bool onSetSpecular(int32_t index, int32_t count, const Color* color);
    virtual bool onSetTexCoord(int32_t index, int32_t count, VertexType type, const Vector2D* texcoord);
};

template<class T>
struct CopyPolicy
{
    static void copy(void* dst, const T* src)
    {
        *((T*)dst) = *src;
    }
};

struct Vector2D2Vector3D
{
    static void copy(void* dst, const Vector2D* src)
    {
        ((Vector3D*)dst)->x = src->x;
        ((Vector3D*)dst)->y = src->y;
        ((Vector3D*)dst)->z = 0.0f;
    }
};

template<class T, class F = CopyPolicy<T> >
struct CopyVertexHelper
{
    static void setVertex(void* dst, int32_t offset, int32_t size, const T* src, int32_t count)
    {
        AL_PRE_COND(dst && 0 <= offset && 0 < size && src && 0 < count, );

        dst = (uint8_t*)dst + offset;

        int32_t i;
        for (i = 0; i < count; ++i, ++src)
        {
            F::copy(dst, src);
            dst = (uint8_t*)dst + size;
        }
    }
};

}

#endif
